package org.texteasy;

import java.util.ArrayList;
import java.util.HashMap;

import android.accounts.AccountManager;
import android.accounts.AuthenticatorDescription;
import android.content.ContentResolver;
import android.content.Context;
import android.content.SyncAdapterType;
import android.content.pm.PackageManager;
import android.provider.ContactsContract;
import android.util.Pair;

public final class Account {

	final private String name;
	final private String type;
	final private String typeLabel;
	final private int iconResourceId;
	
	public Account(String name, String type, String typeLabel, int iconResourceId){
		
		this.name = name;
		this.type = type;
		this.typeLabel = typeLabel;
		this.iconResourceId = iconResourceId;
		
	}

	public String getName() {
		return name;
	}

	public String getType() {
		return type;
	}

	public String getTypeLabel() {
		return typeLabel;
	}

	public int getIconResourceId() {
		return iconResourceId;
	}
	
	public static ArrayList<Account> getAccounts(final Context context){

		ArrayList<Account> toReturn = new ArrayList<Account>();
		
		final ContentResolver resolver = context.getContentResolver();
		final AccountManager am =  AccountManager.get(context.getApplicationContext());

		HashMap<String, Pair<String, Integer>> accountTypeToLabel = getAccountTypeLabelMap(
				context, am);
		
		//get a list of all the active accounts
		final android.accounts.Account[] realAccounts = am.getAccounts();
		for(android.accounts.Account account: realAccounts){
			
			final String name = account.name;
			final String type = account.type;
			
			Pair<String, Integer> authInfo = accountTypeToLabel.get(type);
			String typeLabel = null;
			if(authInfo != null){
				typeLabel = authInfo.first;
			}else{
				typeLabel = type;
			}
			
			int resIcon = -1;
			if(authInfo != null){
				resIcon = authInfo.second;
			}
			
			toReturn.add(new Account(name, type, typeLabel,resIcon));
		}
		
		return toReturn;
	}

	public static HashMap<String, Pair<String, Integer>> getAccountTypeLabelMap(
			final Context context, final AccountManager am) {
		//extract the account package name to label (i.e. com.android.exchange -> Microsoft Exchange ActiveSync)
		HashMap<String, Pair<String, Integer>> accountTypeToLabel = new HashMap<String, Pair<String, Integer>>(); 
		final AuthenticatorDescription[] auths = am.getAuthenticatorTypes();
		for(SyncAdapterType sync: ContentResolver.getSyncAdapterTypes()){
			
			if (!ContactsContract.AUTHORITY.equals(sync.authority)) {
                // Skip sync adapters that don't provide contact data.
                continue;
            }
			
            final AuthenticatorDescription auth = findAuthenticator(auths, sync.accountType);
            //final int iconRes = auth.iconId;
            
            final PackageManager pm = context.getPackageManager();
            CharSequence label = pm.getText(auth.packageName, auth.labelId, null);
            accountTypeToLabel.put(auth.type, Pair.create(label.toString(), auth.iconId));
		}
		return accountTypeToLabel;
	}
	
	/**
     * Find a specific {@link AuthenticatorDescription} in the provided list
     * that matches the given account type.
     */
    protected static AuthenticatorDescription findAuthenticator(AuthenticatorDescription[] auths,
            String accountType) {
        for (AuthenticatorDescription auth : auths) {
            if (accountType.equals(auth.type)) {
                return auth;
            }
        }
        throw new IllegalStateException("Couldn't find authenticator for specific account type");
    }
}
